package 面向对象.battleGame;

import java.util.Random;

public class Role {

    private String name;
    private int hp;

    public Role() {
    }

    public Role(String name, int hp) {
        this.name = name;
        this.hp = hp;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getHp() {
        return hp;
    }

    public void setHp(int hp) {
        this.hp = hp;
    }

    //攻击
    public void attack(Role role) {

        //计算造成的伤害，是0-799之间的随机数
        Random r = new Random();
        int atk = r.nextInt(800);

        //修改被攻击方的生命值
        int newHp = role.getHp() - atk;
        //如果生命值小于0，修改为0
        if (newHp < 0) {
            role.setHp(0);
        } else {
            role.setHp(newHp);
        }

        //输出攻击信息
        System.out.println(this.name + "对" + role.getName() +
                "造成了" + atk + "点伤害，" + role.getName() + "剩余" + role.getHp() + "点生命值");
    }
}
